import Vue from 'vue'
import Router from 'vue-router'
import AsyncRouterFun from './async-router'
import constantRouter from './views/constant'
import Store from '@/store'
Vue.use(Router)
//创建路由
const VueRouterPush = Router.prototype.push
Router.prototype.push = function push (to) {
    return VueRouterPush.call(this, to).catch(err => err)
}
const createRouter = () => {
    return new Router({
        mode:"history",
        scrollBehavior (to, from, savedPosition) {
            if (savedPosition) {
                return savedPosition
            } else {
                if (from.meta.keepAlive) {
                    from.meta.savedPosition = document.body.scrollTop
                }
                return {
                    x: 0,
                    y: to.meta.savedPosition || 0
                }
            }
        },
        routes: constantRouter
    })
}
const AllRouter = createRouter()
AsyncRouterFun.install(AllRouter)
const asyncMenu=Store.getters.userInfo.asyncMenu
asyncMenu && AllRouter.$asyncRouter.formatRoutes(asyncMenu)
// 重置路由
export function resetRouter () {
    const newRouter = createRouter()
    Router.matcher = newRouter.matcher
    AsyncRouterFun.install(Router)
}
export default AllRouter
